package logic

import (
	"UserSrv/global/model"
	"UserSrv/global/pkg"
	"UserSrv/internal/svc"
	"UserSrv/user"
	"context"
	"fmt"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserRestLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserRestLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserRestLogic {
	return &UserRestLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserRestLogic) UserRest(in *user.UserRestRequest) (*user.UserRestResponse, error) {
	// todo: add your logic here and delete this line
	var User = model.User{
		Phone:     in.Phone,
		Password:  pkg.MD5(in.Password),
		VipExpire: time.Now().AddDate(0, 0, -1),
	}
	key := fmt.Sprintf("sms_code_phone:%s:code", in.Phone)
	code := model.UserRDB.Get(model.Ctx, key).Val()
	if code != in.Code {
		return nil, fmt.Errorf("验证码错误")
	}
	if err := model.UserDB.Create(&User).Error; err != nil {
		return nil, err
	}
	return &user.UserRestResponse{
		Id: int64(User.ID),
	}, nil
}
